// \u4e00-\u9fa5] 中文验证规则
import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    point: '',building: '',room: '',
    name: '',
    gender: 0 ,
    mobile: '', 
    idcardFrontUrl: '',
    idcardBackUrl: ''
    // idcardFrontUrl: '/static/images/avatar_1.jpg',
    // idcardBackUrl: '/static/images/avatar_2.jpg',
  },
  // 校验规则
  rules: {
    name: [
      { required: true, message: '业主姓名不能为空!' },
      { pattern: /^[\u4e00-\u9fa5]{2,5}$/, message: '业主姓名只能为中文!' },
    ],
    mobile: [
      { required: true, message: '业主手机号不能为空!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号!' },
    ],
    idcardFrontUrl: [
      { required: true, message: '请上传身份证照片面!' }
    ],
    idcardBackUrl: [
      { required: true, message: '请上传身份证国徽面!' }
    ]},
  async goList() {
    // 错误，直接拦截
    if(!this.validate()) return
    const {__webviewId__ , ...data1} = this.data
    // console.log(data1);
    await wx.http({
      url: '/room',
      method: 'POST',
      data: data1
    })
    // console.log("提交成功！");
    wx.utils.toast("提交成功")
    // 提交
    // wx.reLaunch({
    //   url: '/house_pkg/pages/list/index',
    // })
    // 不删除只会回退！！！
    wx.navigateBack({delta: 4})
  },
  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({ [type]: '' })
  },
  onLoad({point,building,room}){
    // console.log(room);
    this.setData({
      point,building,room
    })
  },
  async uploadIdCardImage(e){
   const res1 = await wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
    })
    // 获取到临时地址
    const imagePath = res1.tempFiles[0].tempFilePath
    // 上传到本地
    const res2 = await wx.http.upload(imagePath,'idCard')
    // console.log(res2);
    // 设置数据上去
    this.setData({
      [e.mark.type]: res2.data.url
    })
    
  },
})
